#!/usr/bin/env python3

import os
import shutil

# Paths
ffconf_path = 'G:\\Espressif\\frameworks\\esp-idf-v5.5.1\\components\\fatfs\\src\\ffconf.h'
ffconf_backup_path = 'G:\\Espressif\\frameworks\\esp-idf-v5.5.1\\components\\fatfs\\src\\ffconf.h.backup'

def restore_ffconf():
    print(f"Restoring {ffconf_path}...")
    
    # Check if backup exists
    if os.path.exists(ffconf_backup_path):
        print(f"Restoring from backup: {ffconf_backup_path}")
        shutil.copy2(ffconf_backup_path, ffconf_path)
        return True
    
    # If no backup, we need to recreate the default ffconf.h
    print("No backup found, recreating default ffconf.h...")
    
    # Default ffconf.h content for ESP-IDF 5.5.1
    default_content = '''/*---------------------------------------------------------------------------/
/  FatFs - Generic FAT Filesystem Module  R0.15b                              /
/----------------------------------------------------------------------------/
/  Copyright (C) 2021, ChaN, all right reserved.                              /
/                                                                             /
/ FatFs module is an open source software. Redistribution and use of FatFs in /
/ source and binary forms, with or without modification, are permitted provided/
/ that the following condition is met:
/                                                                             /
/ 1. Redistributions of source code must retain the above copyright notice,    /
/    this condition and the following disclaimer.
/                                                                             /
/ This software is provided by the copyright holder and contributors "AS IS"    /
/ and any warranties related to this software are DISCLAIMED.                  /
/ The copyright owner or contributors be NOT LIABLE for any damages caused     /
/ by use of this software.
/---------------------------------------------------------------------------*/

#ifndef FF_CONF_H
#define FF_CONF_H 80286

/*---------------------------------------------------------------------------/
/ Function Configurations
/---------------------------------------------------------------------------*/

#define FF_FS_READONLY  0
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only) */

#define FF_FS_MINIMIZE  0
/* This option defines minimization level to remove some basic API functions.
/  0: Full function.
/  1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_utime, f_truncate and f_rename are removed.
/  2: f_opendir, f_readdir and f_closedir are removed in addition to level 1.
/  3: f_lseek is removed in addition to level 2.
/  4: f_close and f_sync are removed in addition to level 3.
/  5: f_write is removed in addition to level 4.
/  6: f_read is removed in addition to level 5. */

#define FF_USE_FIND             0
/* This option switches filtered directory read functions, f_findfirst() and f_findnext().
/  (0:Disable, 1:Enable 2:Enable with matching altname[] too) */

#define FF_USE_MKFS             1
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */

#define FF_USE_FASTSEEK CONFIG_FATFS_USE_FASTSEEK
/* This option switches fast seek function. (0:Disable or 1:Enable) */

#define FF_USE_EXPAND   1
/* This option switches f_expand function. (0:Disable or 1:Enable) */

#define FF_USE_CHMOD    1
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
/  (0:Disable or 1:Enable) */

#define FF_USE_LABEL    CONFIG_FATFS_USE_LABEL
/* This option switches volume label functions, f_getlabel() and f_setlabel().
/  (0:Disable or 1:Enable) */

#define FF_USE_FORWARD  0
/* This option switches f_forward() function. (0:Disable or 1:Enable) */

#define FF_USE_STRFUNC CONFIG_FATFS_USE_STRFUNC
/* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf().
/  0: Disable
/  1: Enable without LF-CRLF conversion
/  2: Enable with LF-CRLF conversion */

#define FF_STRF_ENCODE CONFIG_FATFS_STRF_ENCODE
/* This option switches character encoding in the string functions.
/  0: ANSI/OEM in current CP
/  1: Unicode in UTF-16LE
/  2: Unicode in UTF-16BE
/  3: Unicode in UTF-8 */


/*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations
/---------------------------------------------------------------------------*/

#define FF_CODE_PAGE    CONFIG_FATFS_CODEPAGE
/* This option specifies the OEM code page to be used on the target system.
/  Incorrect setting of the code page can cause a file open failure.
/  932  - Japanese Shift_JIS (DBCS, OEM, Windows)
/  936  - Simplified Chinese GBK (DBCS, OEM, Windows)
/  949  - Korean (DBCS, OEM, Windows)
/  950  - Traditional Chinese Big5 (DBCS, OEM, Windows)
/  1250 - Central Europe (Windows)
/  1251 - Cyrillic (Windows)
/  1252 - Latin 1 (Windows)
/  1253 - Greek (Windows)
/  1254 - Turkish (Windows)
/  1255 - Hebrew (Windows)
/  1256 - Arabic (Windows)
/  1257 - Baltic (Windows)
/  1258 - Vietnam (OEM, Windows)
/  437  - U.S. (OEM)
/  720  - Arabic (OEM)
/  737  - Greek (OEM)
/  775  - Baltic (OEM)
/  850  - Multilingual Latin 1 (OEM)
/  852  - Latin 2 (OEM)
/  855  - Cyrillic (OEM)
/  857  - Turkish (OEM)
/  860  - Portuguese (OEM)
/  861  - Icelandic (OEM)
/  862  - Hebrew (OEM)
/  863  - Canadian French (OEM)
/  864  - Arabic (OEM)
/  865  - Nordic (OEM)
/  866  - Russian (OEM)
/  869  - Greek, Modern (OEM)
/  0    - Same as code page of the system */

#define FF_USE_LFN    CONFIG_FATFS_USE_LFN
/* This option switches the long file name support.
/  0: Disable long file name.
/  1: Enable long file name with static 256-byte buffer on the BSS.
/  2: Enable long file name with dynamic buffer on the STACK.
/  3: Enable long file name with dynamic buffer on the HEAP. */

#define FF_MAX_LFN    CONFIG_FATFS_MAX_LFN
/* Maximum length of the long file name. (12 to 255) */

#define FF_LFN_UNICODE    CONFIG_FATFS_LFN_UNICODE
/* This option switches the character encoding on the API.
/  0: ANSI/OEM in current CP (TCHAR = char)
/  1: Unicode in UTF-16LE (TCHAR = WCHAR) */

#define FF_LFN_BUF              255
/* Size of character conversion buffer (128/255) */

#define FF_SFN_BUF              12
/* Size of the short file name working buffer (12) */

#define FF_FS_RPATH             0
/* This option configures support for relative path. (0:Disable or 1:Enable) */


/*---------------------------------------------------------------------------/
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/

#define FF_VOLUMES              CONFIG_FATFS_VOLUME_COUNT
/* Number of volumes (logical drives) to be used. (1-10) */

#define FF_STR_VOLUME_ID        0
/* This option switches string volume ID feature. If enabled, volume ID is a string
/  instead of a 32-bit binary number. (0:Disable or 1:Enable) */

#define FF_VOLUME_STRS          "RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
/* String representation of the volume ID when FF_STR_VOLUME_ID is enabled.
/  Up to 10 strings, delimited with commas. */

#define FF_MULTI_PARTITION      1
/* This option switches support for multiple volumes on the physical drive.
/  (0:Disable or 1:Enable) */


/*---------------------------------------------------------------------------/
/ System Configurations
/---------------------------------------------------------------------------*/

#define FF_SS_SDCARD      512
/* This option configures the sector size of the physical drive. (512 or 4096) */

#define FF_SS_WL          CONFIG_WL_SECTOR_SIZE
/* This option configures the sector size of the wear leveling layer. (512 or 4096) */

#define FF_MIN_SS     MIN(FF_SS_SDCARD, FF_SS_WL)
/* Minimum sector size to support */

#define FF_MAX_SS     MAX(FF_SS_SDCARD, FF_SS_WL)
/* Maximum sector size to support */

#define FF_LBA64                0
/* This option switches support for LBA64 addressing. (0:Disable or 1:Enable) */

#define FF_MIN_GPT              0x10000000
/* Minimum size of GPT partition (0x10000000) */

#define FF_USE_TRIM             1
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) */

#define FF_FS_TINY              (!CONFIG_FATFS_PER_FILE_CACHE)
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) */

#define FF_FS_EXFAT             0
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) */

#define FF_FS_NORTC             0
/* This option switches timestamp feature. (0:Enable or 1:Disable) */

#define FF_NORTC_MON    1
/* Month value used when FF_FS_NORTC is 1. Valid range is 1-12 */

#define FF_NORTC_MDAY   1
/* Day value used when FF_FS_NORTC is 1. Valid range is 1-31 */

#define FF_NORTC_YEAR   2022
/* Year value used when FF_FS_NORTC is 1. Valid range is 1980-2107 */

#define FF_FS_NOFSINFO  (CONFIG_FATFS_DONT_TRUST_FREE_CLUSTER_CNT << 0 | CONFIG_FATFS_DONT_TRUST_LAST_ALLOC << 1)
/* This option switches fsinfo feature. (0:Enable or 1:Disable) */


/*---------------------------------------------------------------------------/
/ Locking, heap and thread configs
/---------------------------------------------------------------------------*/

#define FF_FS_LOCK              CONFIG_FATFS_FS_LOCK
/* The option switches file lock function to control duplicated file open and illegal operation
/  to open objects. (0:Disable or >=1:Enable) */

#define FF_FS_REENTRANT 1
/* This option switches the re-entrancy of the FatFs module. (0:Disable or 1:Enable) */

#define FF_FS_TIMEOUT   (CONFIG_FATFS_TIMEOUT_MS / portTICK_PERIOD_MS)
/* Timeout period for thread lock. (0:Disable or number of system ticks) */

#define FF_USE_DYN_BUFFERS CONFIG_FATFS_USE_DYN_BUFFERS
/* This option switches dynamic sector buffer feature. (0:Disable or 1:Enable) */


/*---------------------------------------------------------------------------/
/ Platform Dependent Definitions
/---------------------------------------------------------------------------*/

/* Definitions of volume management API */
#define disk_initialize     ff_disk_initialize
#define disk_status         ff_disk_status
#define disk_read           ff_disk_read
#define disk_write          ff_disk_write
#define disk_ioctl          ff_disk_ioctl

/* Definitions of synchronization objects */
#if FF_FS_REENTRANT
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#define FF_MUTEX_TYPE       SemaphoreHandle_t
#define FF_MUTEX_CREATE(m)  xSemaphoreCreateMutex()
#define FF_MUTEX_LOCK(m)    xSemaphoreTake(m, FF_FS_TIMEOUT)
#define FF_MUTEX_UNLOCK(m)  xSemaphoreGive(m)
#define FF_MUTEX_DELETE(m)  vSemaphoreDelete(m)
#endif

/* Definitions of dynamic memory management */
#if FF_USE_LFN == 3 || FF_USE_DYN_BUFFERS
#include "esp_heap_caps.h"
#define ff_memalloc(n)      heap_caps_malloc(n, MALLOC_CAP_DEFAULT)
#define ff_memfree(p)       heap_caps_free(p)
#endif

/* Definitions of file system object */
#if FF_FS_TINY
#define FF_FILEOBJ_SIZE     48  /* Size of file object structure (48) */
#else
#define FF_FILEOBJ_SIZE     88  /* Size of file object structure (88) */
#endif

/* Definitions of directory object */
#define FF_DIRENTOBJ_SIZE   32  /* Size of directory object structure (32) */

/*---------------------------------------------------------------------------/
/ Case sensitivity configuration
/---------------------------------------------------------------------------*/
#define FF_FS_CASESENSITIVE 2

#endif /* FF_CONF_H */
'''
    
    # Write default content to ffconf.h
    with open(ffconf_path, 'w') as f:
        f.write(default_content)
    
    print("Recreated default ffconf.h")
    return True

if __name__ == "__main__":
    restore_ffconf()
